﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

namespace Docs2SWF
{
    public class SWFGenerator
    {
        public static bool FromPDF(string source, string output)
        {
            try
            {
                using (Process p = new Process())
                {
                    XDocument settingsXML = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/xml/settings.xml"));
                    var swftool = from server in settingsXML.Descendants("flexpaper")
                                  select new
                                  {
                                      Path = server.Element("path").Value
                                  };
                    string cmd = HttpContext.Current.Server.MapPath(swftool.SingleOrDefault().Path);
                    string target = output + Path.GetFileName(source).Substring(0, Path.GetFileName(source).LastIndexOf(".")) + ".swf";
                    string args = "  -t " + source + " -s flashversion=9 -o " + target;
                    ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    p.StartInfo = psi;
                    p.Start();
                    p.WaitForExit();
                }
                return true;
            }
            catch { return false; }
        }

        public static bool FromWord(string source, string output)
        {
            try
            {
                using (Process p = new Process())
                {
                    string cmd = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPaper Printer\2\Installation", "AppPath", null);
                    string target = output + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".swf";
                    string args = string.Concat(source, " -o ", target);
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = cmd;
                    psi.Arguments = args;
                    psi.UseShellExecute = false;
                    psi.RedirectStandardInput = false;
                    psi.RedirectStandardOutput = false;
                    psi.CreateNoWindow = true;
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    p.StartInfo = psi;
                    p.Start();
                    p.WaitForExit();
                }
                return true;
            }
            catch { return false; }
        }
    }
}
